## ---------------------------
##
## Script name: MRW_high_hurdles_main.R
##
## Purpose of script: Replicate appendix findings from "High Hurdles: Legislative 
##                    Professionalism and the Effectiveness of Women State 
##                    Legislators" - 2025, JOP
##
## Author: Rob McGrath, Josh Ryan, Tia Wrighten
##
## Date Created: 2025-04-08
##
## ---------------------------

rm(list=ls())

if (!require("pacman")) install.packages("pacman")
pacman::p_load(rstudioapi, tidyverse, plm, AER, texreg, broom, readr, splitstackshape, fixest, xtable)

my.dir <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(my.dir)


d <- read_csv("legislators.csv")


#### App Figure A1


# Add full state names
state_lookup <- tibble(st = state.abb, State = state.name)

d_named <- d %>%
  filter(!is.na(SLES.y), !is.na(ncsl)) %>%
  left_join(state_lookup, by = "st") %>%
  mutate(State = ifelse(is.na(State), st, State))  # fallback for non-standard abbrevs

# Reorder State by median SLES.y
state_medians <- d_named %>%
  group_by(State) %>%
  summarise(median_SLES = median(SLES.y, na.rm = TRUE)) %>%
  arrange(median_SLES)

d_named <- d_named %>%
  mutate(State = factor(State, levels = state_medians$State))

# Plot
ggplot(d_named, aes(x = State, y = SLES.y, fill = ncsl)) +
  geom_boxplot(outlier.shape = 16, outlier.size = 1, alpha = 0.9) +
  scale_fill_manual(values = c("Gold" = "gold", "Gray" = "gray70", "Green" = "limegreen")) +
  coord_cartesian(ylim = c(0, 3)) +  # Truncate y-axis at 3
  labs(
    title = "Appendix Figure A1: Boxplots of SLES by State (Y-Axis Truncated at 3)",
    x = "State", y = "SLES", fill = "Legislature Type"
  ) +
  theme_minimal(base_size = 13) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1))

##### App Table B1

d %>% 
  plm(log_SLES ~ female + year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe1

d %>% 
  plm(log_SLES ~ female  + factor(year), data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe2

d %>% 
  plm(log_SLES ~ female   + factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe3

d %>%   filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + 
        female*log_mediancommvalue_scaled + 
        female*ncsl  +
        yearsserved + mp_member  + ideological_deviance  + year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe4

d %>%    filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + 
        female*log_mediancommvalue_scaled +
        female*ncsl +
        yearsserved + mp_member  + ideological_deviance  +
        factor(year), data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe5

d %>%   filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + 
        female*log_mediancommvalue_scaled +
        female*ncsl  +
        yearsserved + mp_member  + ideological_deviance  + 
        factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe6

fits <- list(sles_basic_dfe1, sles_basic_dfe2, sles_basic_dfe3, sles_basic_dfe4, sles_basic_dfe5, sles_basic_dfe6)

coefs = list(
  "female" = "Female",
  "prop_fem_chamber" = "Female Proportion in Chamber (Scaled to Mean 0)",
  "female_leader" = "Female Leader",
  "log_mediancommvalue_scaled" = "Median Committee Value (Logged) - Scaled",
  "female:prop_fem_chamber" = "Female x Scaled Female Proportion in Chamber",
  "female:female_leader" = "Female x Female Leader",
  "female:ncslGray" = "Female x Hybrid Legislature",
  "female:ncslGreen" = "Female x Professional Legislature",
  "female:log_mediancommvalue_scaled" = "Female x Scaled Median Committee Value",
  "yearsserved" = "Seniority (in Years)",
  "mp_member" = "Majority Party Member",
  "democrat" = "Member is a Democrat",
  "Leader" = "Leader",
  "ideological_deviance" = "Ideological Distance from Party Median",
  "(Intercept)" = "Intercept" 
)

texreg(list(coeftest(sles_basic_dfe1, vcov=vcovHC(sles_basic_dfe1,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe2, vcov=vcovHC(sles_basic_dfe2,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe3, vcov=vcovHC(sles_basic_dfe3,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe4, vcov=vcovHC(sles_basic_dfe4,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe5, vcov=vcovHC(sles_basic_dfe5,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe6, vcov=vcovHC(sles_basic_dfe6,type="HC0",cluster="group"))),
       custom.coef.map = coefs,
       custom.gof.rows = list(
         "N" = c(sapply(fits,function(x) glance(x)$nobs)),
         "District Fixed Effects" = c("yes", "yes", "yes", "yes", "yes", "yes"),
         "Year Fixed Effects" = c("no", "yes", "no", "no", "yes", "no"),
         "State x Year trend" = c("no", "no", "yes", "no", "no", "yes")#, 
       ),
       custom.model.names = c("1", "2", "3", "4", "5", "6"),
       caption = "Sex-Based Differences in Logged State Legislative Effectiveness Scores, 2007-2018",
       table=TRUE,
       digits = 3)


##### App Table B2


d %>%   filter(
  st != "CT" & st != "MA" & st != "KY" & st != "NE", 
  !is.na(log_SLES)                                  
) %>%
  plm(log_mediancommvalue ~ female + year, data=., 
      index=c("districtFE"), model="within") -> pv_basic_dfe1

d %>%   filter(
  st != "CT" & st != "MA" & st != "KY" & st != "NE", 
  !is.na(log_SLES)                                  
) %>%
  plm(log_mediancommvalue ~ female + factor(year), data=., 
      index=c("districtFE"), model="within") -> pv_basic_dfe2

d %>% filter(
  st != "CT" & st != "MA" & st != "KY" & st != "NE", 
  !is.na(log_SLES)                                  
) %>%
  plm(log_mediancommvalue ~ female + factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> pv_basic_dfe3

d %>% filter(
  st != "CT" & st != "MA" & st != "KY" & st != "NE", 
  !is.na(log_SLES)                                  
) %>%
  plm(log_mediancommvalue ~ female*prop_fem_chamber + female*female_leader + female*ncsl + 
        yearsserved + mp_member + ideological_deviance +
        year, data=., 
      index=c("districtFE"), model="within") -> pv_basic_dfe4

d %>% filter(
  st != "CT" & st != "MA" & st != "KY" & st != "NE", 
  !is.na(log_SLES)                                  
) %>%
  plm(log_mediancommvalue ~ female*prop_fem_chamber + female*female_leader + female*ncsl + 
        yearsserved + mp_member + ideological_deviance +
        factor(year), data=., 
      index=c("districtFE"), model="within") -> pv_basic_dfe5

d %>% filter(
  st != "CT" & st != "MA" & st != "KY" & st != "NE", 
  !is.na(log_SLES)                                  
) %>%
  plm(log_mediancommvalue ~ female*prop_fem_chamber + female*female_leader + female*ncsl + 
        yearsserved + mp_member + ideological_deviance +
        factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> pv_basic_dfe6

fits <- list(pv_basic_dfe1, pv_basic_dfe2, pv_basic_dfe3, pv_basic_dfe4, pv_basic_dfe5, pv_basic_dfe6)

coefs = list(
  "female" = "Female",
  "prop_fem_chamber" = "Female Proportion in Chamber (Scaled to Mean 0)",
  "female_leader" = "Female Leader",
  "female:prop_fem_chamber" = "Female x Scaled Female Proportion in Chamber",
  "female:female_leader" = "Female x Female Leader",
  "female:ncslGray" = "Female x Hybrid Legislature",
  "female:ncslGreen" = "Female x Professional Legislature",
  "yearsserved" = "Seniority (in Years)",
  "mp_member" = "Majority Party Member",
  "ideological_deviance" = "Ideological Distance from Party Median",
  "(Intercept)" = "Intercept" 
)

texreg(list(coeftest(pv_basic_dfe1, vcov=vcovHC(pv_basic_dfe1,type="HC0",cluster="group")),
            coeftest(pv_basic_dfe2, vcov=vcovHC(pv_basic_dfe2,type="HC0",cluster="group")),
            coeftest(pv_basic_dfe3, vcov=vcovHC(pv_basic_dfe3,type="HC0",cluster="group")),
            coeftest(pv_basic_dfe4, vcov=vcovHC(pv_basic_dfe4,type="HC0",cluster="group")),
            coeftest(pv_basic_dfe5, vcov=vcovHC(pv_basic_dfe5,type="HC0",cluster="group")),
            coeftest(pv_basic_dfe6, vcov=vcovHC(pv_basic_dfe6,type="HC0",cluster="group"))),
       custom.coef.map = coefs,
       custom.gof.rows = list(
         "N" = c(sapply(fits,function(x) glance(x)$nobs)),
         "District Fixed Effects" = c("yes", "yes", "yes", "yes", "yes", "yes"),
         "Year Fixed Effects" = c("no", "yes", "no", "no", "yes", "no"),
         "State x Year trend" = c("no", "no", "yes", "no", "no", "yes")#, 
       ),
       custom.model.names = c("1", "2", "3", "4", "5", "6"),
       caption = "Sex-Based Differences in Logged Committee Portfolio Values, 2007-2018",
       table=TRUE,
       digits = 3)



##### App Table B3


# Add a post-redistricting indicator
d <- d %>%
  mutate(post_redistricting = case_when(
    # For NJ, VA, MS, LA: Indicator equals 1 for year >= 2011
    st %in% c("NJ", "VA", "MS", "LA") & year >= 2011 ~ 1,
    # For all other states: Indicator equals 1 for year >= 2012
    !st %in% c("NJ", "VA", "MS", "LA") & year >= 2012 ~ 1,
    # Otherwise, the indicator equals 0
    TRUE ~ 0
  ))


d %>% filter(post_redistricting==1) %>% 
  plm(log_SLES ~ female + year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe1

d %>% filter(post_redistricting==1) %>% 
  plm(log_SLES ~ female  + factor(year), data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe2

d %>% filter(post_redistricting==1) %>% 
  plm(log_SLES ~ female   + factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe3

d %>% filter(post_redistricting==1) %>% 
  filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + 
        female*log_rawcommvalues_scaled + 
        female*ncsl  +
        yearsserved + mp_member  + ideological_deviance + year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe4

d %>%  filter(post_redistricting==1) %>% 
  filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + 
        female*log_rawcommvalues_scaled +
        female*ncsl +
        yearsserved + mp_member  + ideological_deviance +
        factor(year), data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe5

d %>% filter(post_redistricting==1) %>% 
  filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + 
        female*log_rawcommvalues_scaled +
        female*ncsl  +
        yearsserved + mp_member  + ideological_deviance +
        factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe6

fits <- list(sles_basic_dfe1, sles_basic_dfe2, sles_basic_dfe3, sles_basic_dfe4, sles_basic_dfe5, sles_basic_dfe6)

coefs = list(
  "female" = "Female",
  "prop_fem_chamber" = "Female Proportion in Chamber (Scaled to Mean 0)",
  "female_leader" = "Female Leader",
  "log_rawcommvalues_scaled" = "Committee Portfolio Value (Logged) - Scaled",
  "female:prop_fem_chamber" = "Female x Scaled Female Proportion in Chamber",
  "female:female_leader" = "Female x Female Leader",
  "female:ncslGray" = "Female x Hybrid Legislature",
  "female:ncslGreen" = "Female x Professional Legislature",
  "female:log_rawcommvalues_scaled" = "Female x Scaled Committee Portfolio Value",
  "yearsserved" = "Seniority (in Years)",
  "mp_member" = "Majority Party Member",
  "democrat" = "Member is a Democrat",
  "ideological_deviance" = "Ideological Distance from Party Median",
  "(Intercept)" = "Intercept" 
)

texreg(list(coeftest(sles_basic_dfe1, vcov=vcovHC(sles_basic_dfe1,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe2, vcov=vcovHC(sles_basic_dfe2,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe3, vcov=vcovHC(sles_basic_dfe3,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe4, vcov=vcovHC(sles_basic_dfe4,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe5, vcov=vcovHC(sles_basic_dfe5,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe6, vcov=vcovHC(sles_basic_dfe6,type="HC0",cluster="group"))),
       custom.coef.map = coefs,
       custom.gof.rows = list(
         "N" = c(sapply(fits,function(x) glance(x)$nobs)),
         "District Fixed Effects" = c("yes", "yes", "yes", "yes", "yes", "yes"),
         "Year Fixed Effects" = c("no", "yes", "no", "no", "yes", "no"),
         "State x Year trend" = c("no", "no", "yes", "no", "no", "yes")#, 
       ),
       custom.model.names = c("1", "2", "3", "4", "5", "6"),
       caption = "Sex-Based Differences in Logged State Legislative Effectiveness Scores, 2011/2012-2018 (Depending on when redistricting occurred)",
       table=TRUE,
       digits = 3)



##### App Table B4

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*squire_index   + female*log_rawcommvalues_scaled +
        yearsserved + mp_member + ideological_deviance + year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe4

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*squire_index  + female*log_rawcommvalues_scaled +
        yearsserved + mp_member + ideological_deviance +
        factor(year), data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe5

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*squire_index   + female*log_rawcommvalues_scaled +
        yearsserved + mp_member + ideological_deviance +
        factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe6


fits <- list(sles_basic_dfe4, sles_basic_dfe5, sles_basic_dfe6)

coefs = list(
  "female" = "Female",
  "prop_fem_chamber" = "Female Proportion in Chamber (Scaled to Mean 0)",
  "female_leader" = "Female Leader",
  "log_rawcommvalues_scaled" = "Committee Portfolio Value (Logged) - Scaled",
  "squire_index" = "Squire Professionalism Index",
  "female:prop_fem_chamber" = "Female x Scaled Female Proportion in Chamber",
  "female:female_leader" = "Female x Female Leader",
  "female:squire_index" = "Female x Squire Professionalism Index",
  "female:log_rawcommvalues_scaled" = "Female x Scaled Committee Portfolio Value",
  "yearsserved" = "Seniority (in Years)",
  "mp_member" = "Majority Party Member",
  "ideological_deviance" = "Ideological Distance from Party Median",
  "(Intercept)" = "Intercept" 
)


texreg(list(coeftest(sles_basic_dfe4, vcov=vcovHC(sles_basic_dfe4,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe5, vcov=vcovHC(sles_basic_dfe5,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe6, vcov=vcovHC(sles_basic_dfe6,type="HC0",cluster="group"))),
       custom.coef.map = coefs,
       custom.gof.rows = list(
         "N" = c(sapply(fits,function(x) glance(x)$nobs)),
         "District Fixed Effects" = c("yes", "yes", "yes"),
         "Year Fixed Effects" = c("no", "yes", "no"),
         "State x Year trend" = c("no", "no", "yes")#, 
       ),
       custom.model.names = c("1", "2", "3"),
       caption = "Sex-Based Differences in Logged State Legislative Effectiveness Scores, 2007-2018",
       table=TRUE,
       digits = 3)


##### App Table B5


d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*leg_salary_yr_avg +
        female*slength_avg_all + female*ncsl_num_staff_per_leg + female*log_rawcommvalues_scaled +
        yearsserved + mp_member + ideological_deviance + year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe4

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*leg_salary_yr_avg +
        female*slength_avg_all + female*ncsl_num_staff_per_leg   + female*log_rawcommvalues_scaled +
        yearsserved + mp_member + ideological_deviance +
        factor(year), data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe5

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*leg_salary_yr_avg +
        female*slength_avg_all + female*ncsl_num_staff_per_leg    + female*log_rawcommvalues_scaled +
        yearsserved + mp_member + ideological_deviance +
        factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe6


fits <- list(sles_basic_dfe4, sles_basic_dfe5, sles_basic_dfe6)

coefs = list(
  "female" = "Female",
  "prop_fem_chamber" = "Female Proportion in Chamber (Scaled to Mean 0)",
  "female_leader" = "Female Leader",
  "log_rawcommvalues_scaled" = "Committee Portfolio Value (Logged) - Scaled",
  "leg_salary_yr_avg" = "Legprof: Leg. Salary (Logged)",
  "slength_avg_all" = "Legprof: Session Length (Logged)",
  "ncsl_num_staff_per_leg" = "Legprof: Staff per Legislator",
  "female:prop_fem_chamber" = "Female x Scaled Female Proportion in Chamber",
  "female:female_leader" = "Female x Female Leader",
  "female:leg_salary_yr_avg" = "Female x Legprof: Logged Leg. Salary",
  "female:slength_avg_all" = "Female x Legprof: Logged Session Length",
  "female:ncsl_num_staff_per_leg" = "Female x Legprof: Staff per Legislator",
  "female:log_rawcommvalues_scaled" = "Female x Scaled Committee Portfolio Value",
  "yearsserved" = "Seniority (in Years)",
  "mp_member" = "Majority Party Member",
  "ideological_deviance" = "Ideological Distance from Party Median",
  "(Intercept)" = "Intercept" 
)


texreg(list(coeftest(sles_basic_dfe4, vcov=vcovHC(sles_basic_dfe4,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe5, vcov=vcovHC(sles_basic_dfe5,type="HC0",cluster="group")),
            coeftest(sles_basic_dfe6, vcov=vcovHC(sles_basic_dfe6,type="HC0",cluster="group"))),
       custom.coef.map = coefs,
       custom.gof.rows = list(
         "N" = c(sapply(fits,function(x) glance(x)$nobs)),
         "District Fixed Effects" = c("yes", "yes", "yes"),
         "Year Fixed Effects" = c("no", "yes", "no"),
         "State x Year trend" = c("no", "no", "yes")#, 
       ),
       custom.model.names = c("1", "2", "3"),
       caption = "Sex-Based Differences in Logged State Legislative Effectiveness Scores, 2007-2018",
       table=TRUE,
       digits = 3)


##### App Table B6

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*ncsl  + 
        female*log_rawcommvalues_scaled +
        yearsserved + mp_member + democrat*female + ideological_deviance + year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe4

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*ncsl + 
        female*log_rawcommvalues_scaled +
        yearsserved + mp_member + democrat*female  + ideological_deviance +
        factor(year), data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe5

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*ncsl  + 
        female*log_rawcommvalues_scaled +
        yearsserved + mp_member + democrat*female + ideological_deviance +
        factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe6


fits <- list(sles_basic_dfe4, sles_basic_dfe5, sles_basic_dfe6)

coefs = list(
  "female" = "Female",
  "prop_fem_chamber" = "Female Proportion in Chamber (Scaled to Mean 0)",
  "female_leader" = "Female Leader",
  "log_rawcommvalues_scaled" = "Committee Portfolio Value (Logged) - Scaled",
  "female:prop_fem_chamber" = "Female x Scaled Female Proportion in Chamber",
  "female:female_leader" = "Female x Female Leader",
  "female:ncslGray" = "Female x Hybrid Legislature",
  "female:ncslGreen" = "Female x Professional Legislature",
  "female:log_rawcommvalues_scaled" = "Female x Scaled Committee Portfolio Value",
  "yearsserved" = "Seniority (in Years)",
  "mp_member" = "Majority Party Member",
  "female:mp_member" = "Female x Majority Party Member",
  "democrat" = "Member is a Democrat", 
  "female:democrat" = "Female x Member is a Democrat",
  "ideological_deviance" = "Ideological Distance from Party Median",
  "(Intercept)" = "Intercept" 
)


texreg(list(
  coeftest(sles_basic_dfe4, vcov=vcovHC(sles_basic_dfe4,type="HC0",cluster="group")),
  coeftest(sles_basic_dfe5, vcov=vcovHC(sles_basic_dfe5,type="HC0",cluster="group")),
  coeftest(sles_basic_dfe6, vcov=vcovHC(sles_basic_dfe6,type="HC0",cluster="group"))),
  custom.coef.map = coefs,
  custom.gof.rows = list(
    "N" = c(sapply(fits,function(x) glance(x)$nobs)),
    "District Fixed Effects" = c("yes", "yes", "yes"),
    "Year Fixed Effects" = c("no", "yes", "no"),
    "State x Year trend" = c("no", "no", "yes")#, 
  ),
  custom.model.names = c("1", "2", "3"),
  caption = "Sex-Based Differences in Logged State Legislative Effectiveness Scores, 2007-2018",
  table=TRUE,
  digits = 3)



##### App Table B7

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*ncsl  + 
        female*log_rawcommvalues_scaled +
        yearsserved*ncsl + mp_member*ncsl  + ideological_deviance*ncsl + year*ncsl, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe4

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*ncsl + 
        female*log_rawcommvalues_scaled +
        yearsserved + mp_member + ideological_deviance +
        factor(year), data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe5

d %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_SLES ~ female*prop_fem_chamber + female*female_leader + female*ncsl  + 
        female*log_rawcommvalues_scaled +
        yearsserved + mp_member  + ideological_deviance +
        factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> sles_basic_dfe6


fits <- list(sles_basic_dfe4, sles_basic_dfe5, sles_basic_dfe6)

coefs = list(
  "female" = "Female",
  #"prop_fem_chamber" = "Female Proportion in Chamber (Scaled to Mean 0)",
  #"female_leader" = "Female Leader",
  #"log_rawcommvalues_scaled" = "Committee Portfolio Value (Logged) - Scaled",
  #"female:prop_fem_chamber" = "Female x Scaled Female Proportion in Chamber",
  #"female:female_leader" = "Female x Female Leader",
  "female:ncslGray" = "Female x Hybrid Legislature",
  "female:ncslGreen" = "Female x Professional Legislature",
  #"female:log_rawcommvalues_scaled" = "Female x Scaled Committee Portfolio Value",
  #"yearsserved" = "Seniority (in Years)",
  #"mp_member" = "Majority Party Member",
  #"female:mp_member" = "Female x Majority Party Member",
  #"democrat" = "Member is a Democrat", 
  #"female:democrat" = "Female x Member is a Democrat",
  #"ideological_deviance" = "Ideological Distance from Party Median",
  "(Intercept)" = "Intercept" 
)


texreg(list(
  coeftest(sles_basic_dfe4, vcov=vcovHC(sles_basic_dfe4,type="HC0",cluster="group")),
  coeftest(sles_basic_dfe5, vcov=vcovHC(sles_basic_dfe5,type="HC0",cluster="group")),
  coeftest(sles_basic_dfe6, vcov=vcovHC(sles_basic_dfe6,type="HC0",cluster="group"))),
  custom.coef.map = coefs,
  custom.gof.rows = list(
    "N" = c(sapply(fits,function(x) glance(x)$nobs)),
    "Control Variables Interacted with Leg. Prof." = c("yes", "yes", "yes"),
    "District Fixed Effects" = c("yes", "yes", "yes"),
    "Year Fixed Effects" = c("no", "yes", "no"),
    "State x Year trend" = c("no", "no", "yes")#, 
  ),
  custom.model.names = c("1", "2", "3"),
  caption = "Sex-Based Differences in Logged State Legislative Effectiveness Scores, 2007-2018",
  table=TRUE,
  digits = 3)



###### App Table B8
#### this involves the legislative term unit of analysis

d_term <- d %>%
  # Create the term column
  mutate(
    term = case_when(
      st %in% c("LA", "MS", "NJ", "VA") & year %% 2 == 1 ~ paste0(year, "_", year + 1),  # Odd-year states
      !(st %in% c("LA", "MS", "NJ", "VA")) & year %% 2 == 0 ~ paste0(year, "_", year + 1) # Even-year states
    ),
    term_numeric = as.numeric(substr(term, 1, 4))  # Extract the start year as a numeric value
  ) %>%
  # Filter out terms based on numeric start year
  filter(
    (st %in% c("LA", "MS", "NJ", "VA") & term_numeric >= 2007) |  # Odd-year states, start >= 2007
      (!(st %in% c("LA", "MS", "NJ", "VA")) & term_numeric >= 2008) # Even-year states, start >= 2008
  ) %>%
  # Group by klarner_id, districtFE, term, and st
  group_by(klarner_id, districtFE, term, st) %>%
  # Aggregate variables for each legislator-district-term combination
  summarise(
    log_SLES = first(log_SLES),  # Assuming log_SLES does not vary within a term
    female = mean(female, na.rm = TRUE),
    prop_fem_chamber = mean(prop_fem_chamber, na.rm = TRUE),
    female_leader = mean(female_leader, na.rm = TRUE),
    log_rawcommvalues_scaled = mean(log_rawcommvalues_scaled, na.rm = TRUE),
    ncsl = first(ncsl),  # Categorical variable, assuming it doesn’t vary
    yearsserved = mean(yearsserved, na.rm = TRUE),
    mp_member = mean(mp_member, na.rm = TRUE),
    ideological_deviance = mean(ideological_deviance, na.rm = TRUE)
  ) %>%
  ungroup()


# Define the models using the term-level dataset
d_term %>% filter(term != "2006_2007") %>%
  plm(log_SLES ~ female + term, data = ., 
      index = c("districtFE"), model = "within") -> sles_term_dfe1

d_term %>%
  plm(log_SLES ~ female + factor(term), data = ., 
      index = c("districtFE"), model = "within") -> sles_term_dfe2

d_term %>%
  plm(log_SLES ~ female + factor(st) * term, data = ., 
      index = c("districtFE"), model = "within") -> sles_term_dfe3

d_term %>% 
  filter(st != "CT" & st != "MA" & st != "KY" & st != "NE") %>%
  plm(log_SLES ~ female * prop_fem_chamber + female * female_leader + 
        female * log_rawcommvalues_scaled + 
        female * ncsl +
        yearsserved + mp_member + ideological_deviance + term, data = ., 
      index = c("districtFE"), model = "within") -> sles_term_dfe4

d_term %>% 
  filter(st != "CT" & st != "MA" & st != "KY" & st != "NE") %>%
  plm(log_SLES ~ female * prop_fem_chamber + female * female_leader + 
        female * log_rawcommvalues_scaled +
        female * ncsl +
        yearsserved + mp_member + ideological_deviance +
        factor(term), data = ., 
      index = c("districtFE"), model = "within") -> sles_term_dfe5

d_term %>% 
  filter(st != "CT" & st != "MA" & st != "KY" & st != "NE") %>%
  plm(log_SLES ~ female * prop_fem_chamber + female * female_leader + 
        female * log_rawcommvalues_scaled +
        female * ncsl +
        yearsserved + mp_member + ideological_deviance + 
        factor(st) * term, data = ., 
      index = c("districtFE"), model = "within") -> sles_term_dfe6

fits_term <- list(
  sles_term_dfe1, sles_term_dfe2, sles_term_dfe3, 
  sles_term_dfe4, sles_term_dfe5, sles_term_dfe6
)

coefs_term <- list(
  "female" = "Female",
  "prop_fem_chamber" = "Female Proportion in Chamber (Scaled to Mean 0)",
  "female_leader" = "Female Leader",
  "log_rawcommvalues_scaled" = "Committee Portfolio Value (Logged) - Scaled",
  "female:prop_fem_chamber" = "Female x Scaled Female Proportion in Chamber",
  "female:female_leader" = "Female x Female Leader",
  "female:ncslGray" = "Female x Hybrid Legislature",
  "female:ncslGreen" = "Female x Professional Legislature",
  "female:log_rawcommvalues_scaled" = "Female x Scaled Committee Portfolio Value",
  "yearsserved" = "Seniority (in Years)",
  "mp_member" = "Majority Party Member",
  "democrat" = "Member is a Democrat",
  "Leader" = "Leader",
  "ideological_deviance" = "Ideological Distance from Party Median",
  "(Intercept)" = "Intercept"
)

texreg(list(coeftest(sles_term_dfe1, vcov = vcovHC(sles_term_dfe1, type = "HC0", cluster = "group")),
            coeftest(sles_term_dfe2, vcov = vcovHC(sles_term_dfe2, type = "HC0", cluster = "group")),
            coeftest(sles_term_dfe3, vcov = vcovHC(sles_term_dfe3, type = "HC0", cluster = "group")),
            coeftest(sles_term_dfe4, vcov = vcovHC(sles_term_dfe4, type = "HC0", cluster = "group")),
            coeftest(sles_term_dfe5, vcov = vcovHC(sles_term_dfe5, type = "HC0", cluster = "group")),
            coeftest(sles_term_dfe6, vcov = vcovHC(sles_term_dfe6, type = "HC0", cluster = "group"))),
       custom.coef.map = coefs_term,
       custom.gof.rows = list(
         "N" = c(sapply(fits_term, function(x) glance(x)$nobs)),
         "District Fixed Effects" = c("yes", "yes", "yes", "yes", "yes", "yes"),
         "Term Fixed Effects" = c("no", "yes", "no", "no", "yes", "no"),
         "State x Term trend" = c("no", "no", "yes", "no", "no", "yes")
       ),
       custom.model.names = c("1", "2", "3", "4", "5", "6"),
       caption = "Sex-Based Differences in Logged State Legislative Effectiveness Scores by Term, 2007-2018",
       table = TRUE,
       digits = 3)


##### App Figure B1

# Add term limits indicator
d <- d %>%
  mutate(term_limits = ifelse(st %in% c("AZ", "CA", "CO", "FL", "ME", "MI", "MO", "NE", "NV", "OH", "OK", "SD"), 1, 0))


d %>%
  filter(!st %in% c("CT", "MA", "KY", "NE")) %>%
  plm(
    log_SLES ~ female * ncsl * term_limits +
      female * prop_fem_chamber +
      female * female_leader +
      female * log_mediancommvalue_scaled +
      yearsserved + mp_member + ideological_deviance +
      factor(st) * year,
    data = .,
    index = c("districtFE"),
    model = "within"
  ) -> sles_with_tl_interaction


# Compute marginal effects for TL and non-TL states
coef_base <- coef(sles_with_tl_interaction)
vcov_base <- vcovHC(sles_with_tl_interaction, type = "HC0", cluster = "group")

# Compute marginal effects for TL and non-TL states
marginal_effects <- data.frame(
  Legislature = c("Amateur Legislatures", "Hybrid Legislatures", "Professional Legislatures"),
  TL = c(
    coef_base["female"] + coef_base["female:term_limits"],
    coef_base["female"] + coef_base["female:ncslGray"] + coef_base["female:term_limits"] + coef_base["female:ncslGray:term_limits"],
    coef_base["female"] + coef_base["female:ncslGreen"] + coef_base["female:term_limits"] + coef_base["female:ncslGreen:term_limits"]
  ),
  Non_TL = c(
    coef_base["female"],
    coef_base["female"] + coef_base["female:ncslGray"],
    coef_base["female"] + coef_base["female:ncslGreen"]
  )
)

# Compute standard errors for TL and Non-TL marginal effects
se_TL <- c(
  sqrt(vcov_base["female", "female"] + vcov_base["female:term_limits", "female:term_limits"] + 2 * vcov_base["female", "female:term_limits"]),
  sqrt(vcov_base["female", "female"] + vcov_base["female:ncslGray", "female:ncslGray"] +
         vcov_base["female:term_limits", "female:term_limits"] + vcov_base["female:ncslGray:term_limits", "female:ncslGray:term_limits"] +
         2 * vcov_base["female", "female:term_limits"] +
         2 * vcov_base["female", "female:ncslGray"] +
         2 * vcov_base["female:term_limits", "female:ncslGray:term_limits"]),
  sqrt(vcov_base["female", "female"] + vcov_base["female:ncslGreen", "female:ncslGreen"] +
         vcov_base["female:term_limits", "female:term_limits"] + vcov_base["female:ncslGreen:term_limits", "female:ncslGreen:term_limits"] +
         2 * vcov_base["female", "female:term_limits"] +
         2 * vcov_base["female", "female:ncslGreen"] +
         2 * vcov_base["female:term_limits", "female:ncslGreen:term_limits"])
)

se_Non_TL <- c(
  sqrt(vcov_base["female", "female"]),
  sqrt(vcov_base["female", "female"] + vcov_base["female:ncslGray", "female:ncslGray"] + 2 * vcov_base["female", "female:ncslGray"]),
  sqrt(vcov_base["female", "female"] + vcov_base["female:ncslGreen", "female:ncslGreen"] + 2 * vcov_base["female", "female:ncslGreen"])
)

# Add confidence intervals to the data frame
marginal_effects <- marginal_effects %>%
  mutate(
    TL_Lower_CI = TL - qnorm(0.95) * se_TL,
    TL_Upper_CI = TL + qnorm(0.95) * se_TL,
    Non_TL_Lower_CI = Non_TL - qnorm(0.95) * se_Non_TL,
    Non_TL_Upper_CI = Non_TL + qnorm(0.95) * se_Non_TL
  )

# Add confidence intervals directly into a long-format data frame
plot_data <- marginal_effects %>%
  pivot_longer(
    cols = c(TL, Non_TL),
    names_to = "Term_Limits",
    values_to = "Marginal_Effect"
  ) %>%
  mutate(
    Lower_CI = ifelse(Term_Limits == "TL", TL_Lower_CI, Non_TL_Lower_CI),
    Upper_CI = ifelse(Term_Limits == "TL", TL_Upper_CI, Non_TL_Upper_CI)
  )


# Reorder the Legislature factor
plot_data <- plot_data %>%
  mutate(
    Legislature = factor(
      Legislature,
      levels = c("Professional Legislatures", "Hybrid Legislatures", "Amateur Legislatures")  # Reverse order
    )
  )

# Plot with the updated order
ggplot(plot_data, aes(x = Marginal_Effect, y = Legislature, color = Term_Limits)) +
  geom_point(size = 3, position = position_dodge(width = 0.3)) +  # Add points with slight separation
  geom_errorbarh(
    aes(xmin = Lower_CI, xmax = Upper_CI),
    height = 0.2,
    position = position_dodge(width = 0.3)
  ) +  # Add horizontal error bars
  geom_vline(xintercept = 0, linetype = "dashed") +  # Add a vertical reference line at 0
  labs(
    title = "Marginal Effects of Female on Legislative Effectiveness by Term Limit Status",
    x = "Marginal Effect",
    y = "",
    color = "Term Limit Status"  # Change legend title
  ) +
  scale_color_manual(  # Customize legend labels and colors
    values = c("TL" = "#1f78b4", "Non_TL" = "#33a02c"),  # Change colors
    labels = c("TL" = "Term-Limited States", "Non_TL" = "Non-Term-Limited States")  # Change labels
  ) +
  theme_minimal() +  # Use a clean theme
  theme(
    axis.text.y = element_text(size = 10),
    axis.title.x = element_text(size = 12),
    plot.title = element_text(hjust = 0.5, size = 14),
    legend.position = "bottom",  # Move legend to the bottom
    legend.title = element_text(size = 12, face = "bold"),  # Customize legend title
    legend.text = element_text(size = 10)  # Customize legend labels
  )





#### App Table C1 -- committees.csv


comms <- read.csv("committees.csv")

comms$st <- substr(comms$comm.name, 1, 2)
comms$cham <- substr(comms$comm.name, 3, 3)


comms <- comms %>% filter(st != "CT" & st != "MA" & st != "KY" & st != "NE") 
  
  
summary_table <- comms %>%
  group_by(st) %>%
  summarise(
    Mean = mean(comm.value, na.rm = TRUE),
    Median = median(comm.value, na.rm = TRUE),
    SD = sd(comm.value, na.rm = TRUE),
    Min = min(comm.value, na.rm = TRUE),
    Max = max(comm.value, na.rm = TRUE),
    .groups = "drop"
  )

summary_table_rounded <- summary_table %>%
  mutate(across(Mean:Max, ~ round(.x, 3)))

latex_lines <- c(
  "\\begin{table}[htp!]",
  "\\footnotesize",
  "\\centering",
  "\\begin{tabularx}{.8\\textwidth}{XXXXXcc}",
  "\\toprule",
  "state & Mean & Median & SD & Min & Max\\\\",
  "\\midrule"
)

for (i in seq_len(nrow(summary_table_rounded))) {
  row <- summary_table_rounded[i, ]
  line <- sprintf(
    "%s & %.3f & %.3f & %.3f & %.3f & %.3f\\\\",
    row$st, row$Mean, row$Median, row$SD, row$Min, row$Max
  )
  latex_lines <- c(latex_lines, line)
  if (i %% 5 == 0 && i != nrow(summary_table_rounded)) {
    latex_lines <- c(latex_lines, "\\addlinespace")
  }
}

latex_lines <- c(
  latex_lines,
  "\\bottomrule\\end{tabularx}",
  "\\caption*{Appendix Table C1: Summary of ``Grosewart'' Committee Values by State}",
  "\\end{table}"
)

cat(paste(latex_lines, collapse = "\n"))




#### App Table C2 - back to legislators.csv

d <- d %>%
  mutate(rawcommvalues = exp(log_rawcommvalues))

summary_table <- d %>%
  filter(st != "CT" & st != "MA" & st != "KY" & st != "NE") %>%
  group_by(st) %>%
  summarise(
    Mean = mean(rawcommvalues, na.rm = TRUE),
    Median = median(rawcommvalues, na.rm = TRUE),
    SD = sd(rawcommvalues, na.rm = TRUE),
    Min = min(rawcommvalues, na.rm = TRUE),
    Max = max(rawcommvalues, na.rm = TRUE),
    .groups = "drop"
  )

summary_table_rounded <- summary_table %>%
  mutate(across(Mean:Max, ~ round(.x, 3)))

latex_lines <- c(
  "\\begin{table}[htp!]",
  "\\footnotesize",
  "\\centering",
  "\\begin{tabularx}{.8\\textwidth}{XXXXXcc}",
  "\\toprule",
  "state & Mean & Median & SD & Min & Max\\\\",
  "\\midrule"
)

for (i in seq_len(nrow(summary_table_rounded))) {
  row <- summary_table_rounded[i, ]
  line <- sprintf(
    "%s & %.3f & %.3f & %.3f & %.3f & %.3f\\\\",
    row$st, row$Mean, row$Median, row$SD, row$Min, row$Max
  )
  latex_lines <- c(latex_lines, line)
  if (i %% 5 == 0 && i != nrow(summary_table_rounded)) {
    latex_lines <- c(latex_lines, "\\addlinespace")
  }
}

latex_lines <- c(
  latex_lines,
  "\\bottomrule\\end{tabularx}",
  "\\caption*{Appendix Table C2: Summary of Committee Portfolio Values by State}",
  "\\end{table}"
)

cat(paste(latex_lines, collapse = "\n"))





#### App Table C3 

states_lu <- data.frame(State = state.name, State_abb = state.abb)


sex_regs <- function(states) {
  
  # Prep house data
  house_data <- d %>%
    filter(state == states & chamber == "house") %>%
    cSplit_e("comms", sep = "|", type = "character", fill = 0) %>%
    select(starts_with("comms_"), female, year, districtFE)
  
  house_depvars <- names(house_data)[grepl("^comms_", names(house_data))]
  
  house_results <- map_dfr(house_depvars, function(var) {
    fit <- tryCatch({
      feols(xpd(..lhs ~ female + year | districtFE, ..lhs = var), data = house_data)
    }, error = function(e) NULL)
    
    if (is.null(fit)) return(NULL)
    
    tidy_fit <- tryCatch(tidy(fit), error = function(e) NULL)
    if (is.null(tidy_fit) || !"female" %in% tidy_fit$term) return(NULL)
    
    tidy_fit <- tidy_fit %>% filter(term == "female") %>%
      mutate(Committee = gsub("comms_", "", var),
             State = states,
             Chamber = "house") %>%
      select(State, Chamber, Committee, estimate, std.error, statistic, p.value) %>%
      rename(Coef = estimate, SE = std.error, Statistic = statistic, PValue = p.value)
    
    tidy_fit
  })
  
  # Prep senate data
  senate_data <- d %>%
    filter(state == states & chamber == "senate") %>%
    cSplit_e("comms", sep = "|", type = "character", fill = 0) %>%
    select(starts_with("comms_"), female, year, districtFE)
  
  senate_depvars <- names(senate_data)[grepl("^comms_", names(senate_data))]
  
  senate_results <- map_dfr(senate_depvars, function(var) {
    fit <- tryCatch({
      feols(xpd(..lhs ~ female + year | districtFE, ..lhs = var), data = senate_data)
    }, error = function(e) NULL)
    
    if (is.null(fit)) return(NULL)
    
    tidy_fit <- tryCatch(tidy(fit), error = function(e) NULL)
    if (is.null(tidy_fit) || !"female" %in% tidy_fit$term) return(NULL)
    
    tidy_fit <- tidy_fit %>% filter(term == "female") %>%
      mutate(Committee = gsub("comms_", "", var),
             State = states,
             Chamber = "senate") %>%
      select(State, Chamber, Committee, estimate, std.error, statistic, p.value) %>%
      rename(Coef = estimate, SE = std.error, Statistic = statistic, PValue = p.value)
    
    tidy_fit
  })
  
  # Combine
  bind_rows(house_results, senate_results)
}



todo <- states_lu$State
todo <- todo[-c(7,17,21,27)]

result <- map_dfr(todo, sex_regs)

result <- result %>% filter(Statistic > 1.96 | Statistic < (-1.96))

result$WomenSelection <- as.integer(grepl(
  pattern = "health|hosp|medic|educ|school|univer|teach",
  x = tolower(result$Committee)
))

result <- result %>%
  mutate(WomenSelection = ifelse(Coef < 0, 0, WomenSelection))

result$MenSelection <- as.integer(grepl(
  pattern = "ban|fin|comme|rule",
  x = tolower(result$Committee)
))


result <- result %>%
  mutate(MenSelection = ifelse(Coef > 0, 0, MenSelection))

chambers_with_selection <- result %>%
  filter(WomenSelection == 1 ) %>%
  select(State, Chamber) %>%
  distinct()


names(chambers_with_selection) <- c("state", "chamber")

d_no_selection <- d %>%
  anti_join(chambers_with_selection, by = c("state", "chamber"))

d_with_selection <- d %>%
  semi_join(chambers_with_selection, by = c("state", "chamber"))

rownames(chambers_with_selection) <- NULL

print(xtable(chambers_with_selection),type = "latex", include.rownames=FALSE)


#### App Table C4 

d_no_selection  %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_rawcommvalues ~ female*prop_fem_chamber + female*female_leader + female*ncsl + 
        yearsserved + mp_member + ideological_deviance +
        year, data=., 
      index=c("districtFE"), model="within") -> pv_basic_dfe1


d_no_selection  %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_rawcommvalues ~ female*prop_fem_chamber + female*female_leader + female*ncsl + 
        yearsserved + mp_member + ideological_deviance +
        factor(year), data=., 
      index=c("districtFE"), model="within") -> pv_basic_dfe2

d_no_selection  %>% filter(st!= "CT" & st!="MA" & st!="KY" & st!="NE") %>% 
  plm(log_rawcommvalues ~ female*prop_fem_chamber + female*female_leader + female*ncsl + 
        yearsserved + mp_member + ideological_deviance +
        factor(st)*year, data=., 
      index=c("districtFE"), model="within") -> pv_basic_dfe3

fits <- list(pv_basic_dfe1, pv_basic_dfe2, pv_basic_dfe3)

coefs = list(
  "female" = "Female",
  "prop_fem_chamber" = "Female Proportion in Chamber (Scaled to Mean 0)",
  "female_leader" = "Female Leader",
  "log_rawcommvalues_scaled" = "Raw Committee Values (Logged) - Scaled",
  "female:prop_fem_chamber" = "Female x Scaled Female Proportion in Chamber",
  "female:female_leader" = "Female x Female Leader",
  "female:ncslGray" = "Female x Hybrid Legislature",
  "female:ncslGreen" = "Female x Professional Legislature",
  "female:log_rawcommvalues_scaled" = "Female x Scaled Raw Committee Values",
  "yearsserved" = "Seniority (in Years)",
  "mp_member" = "Majority Party Member",
  "ideological_deviance" = "Ideological Distance from Party Median",
  "(Intercept)" = "Intercept" 
)


texreg(list(coeftest(pv_basic_dfe1, vcov=vcovHC(pv_basic_dfe1,type="HC0",cluster="group")),
            coeftest(pv_basic_dfe2, vcov=vcovHC(pv_basic_dfe2,type="HC0",cluster="group")),
            coeftest(pv_basic_dfe3, vcov=vcovHC(pv_basic_dfe3,type="HC0",cluster="group"))),
       custom.coef.map = coefs,
       custom.gof.rows = list(
         "N" = c(sapply(fits,function(x) glance(x)$nobs)),
         "District Fixed Effects" = c("yes", "yes", "yes"),
         "Year Fixed Effects" = c("no", "yes", "no"),
         "State x Year trend" = c("no", "no", "yes")#, 
       ),
       custom.model.names = c("1", "2", "3"),
       caption = "Sex-Based Differences in Logged Committee Portfolio Values, Accounting for Theoretical Moderators (Chambers with No Evidence of Sex-Based Self-Selection to Committees)",
       table=TRUE,
       digits = 5)



#### App Table C5

df <- read.csv("congress_committees.csv")

df <- df %>%
  mutate(
    OverallRank = rank(-Overall.Score, ties.method = "first"),
    WomenRank = rank(-Women, ties.method = "first"),
    MenRank = rank(-Men, ties.method = "first"),
    OverallPrint = paste0(round(Overall.Score, 2), " (", OverallRank, ")"),
    WomenPrint = paste0(round(Women, 2), " (", WomenRank, ")"),
    MenPrint = paste0(round(Men, 2), " (", MenRank, ")")
  ) %>%
  arrange(OverallRank)

latex_rows <- apply(df, 1, function(row) {
  paste0(row["Stewart.Comm..Id."], "\t&\t",
         row["Committee.Name"], "\t&\t",
         row["OverallPrint"], "\t&\t",
         row["WomenPrint"], "\t&\t",
         row["MenPrint"], "\t\\\\[\\defaultaddspace]")
})

latex_table <- c(
  "\\begin{table}[htp!]",
  "\\centering",
  "\\caption*{Appendix Table C5: Congressional Committee Grosewart Scores by Legislator Sex}",
  "\\label{cong_grosewart}",
  "\\begin{tabular}{l c c c c c}",
  "\\multicolumn{1}{c}{\\textbf{Stewart}} & \\multicolumn{1}{c}{\\textbf{Committee}} & \\multicolumn{1}{c}{\\textbf{Overall}} & \\multicolumn{1}{c}{\\textbf{Women}} & \\multicolumn{1}{c}{\\textbf{Men}} \\\\",
  "\\multicolumn{1}{c}{\\textbf{Comm. Id.}} & \\multicolumn{1}{c}{\\textbf{Name}}  & \\multicolumn{1}{c}{\\textbf{Score}} & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{} \\\\",
  "\\hline",
  "\\hline",
  latex_rows,
  "\\bottomrule",
  "\\end{tabular}",
  "\\end{table}"
)

cat(paste(latex_table, collapse = "\n"))


